package com.project.forlearningdemo.utils;

import com.alibaba.fastjson.JSONObject;
import com.project.forlearningdemo.pojo.Comment;
import com.project.forlearningdemo.pojo.Resource;
import com.project.forlearningdemo.pojo.User;
import com.project.forlearningdemo.pojo.vo.Condition;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 字符串工具类，用于字符串的使用场景
 */
public class StringUtils {
    //密匙
    public static final String KEY = "xaosihdqwsd";
    //空列
    public static List list = new ArrayList();
    /**
     * 判断字符串是否为空或者空串 如果有一个str为空，那么表示为空，返回true
     *
     */
    public static boolean isEmpty(String... str) {
        if (str == null) return true;

        for (String s : str) {
            if (s == null || "".equals(s.replace(" ", ""))) return true;
        }
        return false;
    }

    /**
     * 加密:返回加密后的密码
     */
    public static String tranPass(String adminPass) {
        String str = adminPass + KEY;
        byte[] strByte = str.getBytes(StandardCharsets.UTF_8);
        return DigestUtils.md5DigestAsHex(strByte);
    }

    /**
     * 生成账号
     */
    public static String createName() {
        return String.valueOf(new Date().getTime() / 1000);
    }

    /**
     * 生成验证码
     */
    public static String createCode() {
        return UUID.randomUUID().toString().substring(0, 6);
    }

    /**
     * 处理标签
     */
    public static List<String> makeLabels(String labels) {
        if(labels == null) return null;
        return List.of((labels + ";").split(";"));
    }

    /**
     * 简化传值 负责直接将JSON字符串转换并且封装为Condition对象
     */
    public static Object tranJSON(String json, Object obj){
        JSONObject jsonObject = JSONObject.parseObject(json);

        if(obj instanceof Condition condition){

            condition.setResourceId(jsonObject.getInteger("resourceId"));
            condition.setUserId(jsonObject.getInteger("userId"));
            condition.setStr(jsonObject.getString("str"));
            condition.setLabels(jsonObject.getString("labels"));
            condition.setStatus(jsonObject.getString("status"));
            condition.setIsOr( jsonObject.getBoolean("isOr"));
            condition.setOrderBy(jsonObject.getString("orderBy"));
            condition.setIsDesc( jsonObject.getBoolean("isDesc"));
            condition.setPageNum( jsonObject.getInteger("pageNum"));
            condition.setIsS( jsonObject.getBoolean("isS"));
            condition.setRandom( jsonObject.getBoolean("random"));
            return condition;
        }else if(obj instanceof Resource resource){

            User user = new User();
            user.setUserId(jsonObject.getInteger("userId"));
            resource.setUser(user);
            resource.setResourceId(jsonObject.getInteger("resourceId"));
            resource.setTitle( jsonObject.getString("title"));
            resource.setIntroduction(jsonObject.getString("introduction"));
            resource.setUrl( jsonObject.getString("url"));
            resource.setLabels(jsonObject.getString("labels"));
            resource.setIcon(jsonObject.getString("icon"));
            resource.setDetails( jsonObject.getString("details"));
            resource.setCollectionNum( jsonObject.getInteger("collectionNum"));
            resource.setScanNum(jsonObject.getInteger("scanNum"));
            resource.setStarNum(jsonObject.getInteger("starNum"));

            return resource;
        }else if(obj instanceof User user){

            user.setUserId( jsonObject.getInteger("userId"));
            user.setName( jsonObject.getString("name"));
            user.setAuth( jsonObject.getString("auth"));
            user.setHeadImage( jsonObject.getString("headImage"));
            user.setIntroduction( jsonObject.getString("introduction"));
            user.setMail( jsonObject.getString("mail"));
            user.setHabby( jsonObject.getString("habby"));
            user.setAddress( jsonObject.getString("address"));
            return user;
        }else if(obj instanceof Comment comment){

            comment.setCommentId( jsonObject.getInteger("commentId"));
            comment.setResourceId( jsonObject.getInteger("resourceId"));
            comment.setUserId( jsonObject.getInteger("userId"));
            comment.setStarNum( jsonObject.getInteger("starNum"));
            comment.setCommentInfo( jsonObject.getString("commentInfo"));
            comment.setToCommentId( jsonObject.getInteger("toCommentId"));
            return comment;
        }
        throw new NullPointerException();
    }


}